/*****************************************************************************\

File:   otkDrawing.java

Date          Build     Author        Version   Comments
28-Aug-12    P-20-12    asonar         $$1       Created
19-Oct-12    P-20-15    asonar         $$2       Updated for qcr
20-mar-15 P-30-05 gshmelev $$3  called GetCurrentSession

\****************************************************************************/

package com.ptc.otkjavaexamples;

import com.ptc.cipjava.*;
import com.ptc.pfc.pfcSession.*;
import com.ptc.pfc.pfcCommand.*;
import com.ptc.pfc.pfcGlobal.*;
import com.ptc.pfc.pfcModel.*;
import com.ptc.pfc.pfcModelItem.*;
import com.ptc.pfc.pfcFeature.*;
import com.ptc.pfc.pfcSolid.*;
import java.io.*;

import com.ptc.wfc.wfcSession.*;
import com.ptc.pfc.pfcDrawing.*;
import com.ptc.pfc.pfcModel2D.Model2D.*;
import com.ptc.pfc.pfcBase.*;
import com.ptc.pfc.pfcFamily.*;
import com.ptc.pfc.pfcWindow.*;
import com.ptc.pfc.pfcView2D.*; 

/*====================================================================*\
  FUNCTION : otkDrawingCreateSheetAndViews()
  PURPOSE  : Create a new drawing sheet with a general, and two
  projected, views of a selected solid
  \*====================================================================*/

class otkDrawing extends DefaultUICommandActionListener
{
    //static String dwgtxt = "otkJavaMessage.txt"; 
    
    public otkDrawing()
    {
    }
    
    public void OnCommand()
    {
	addDrawing();
    }
    
    public void addDrawing()
    {
	try
	    {
	
		FileWriter file = new FileWriter ("Drawing.txt");
		BufferedWriter fp_out_Drawing = new BufferedWriter(file);	
		
		Session session = pfcSession.GetCurrentSession();
		WSession ses = (WSession)session;
		
		Model model = ses.GetCurrentModel();
		
		ses.UIDisplayMessage ("dwg_1.txt", "OTK_DWG1", null);
		
		String solidName = ses.UIReadStringMessage (null);
		
		otkDrawingCreateSheetAndViews(model, solidName, session, fp_out_Drawing);
		
		fp_out_Drawing.close();
		
	    }
	
	catch (Exception e)
	    {
		System.out.println("Exception: " + e);
	    }
    }
    
    void otkDrawingCreateSheetAndViews (Model model, String solidName, Session session, BufferedWriter fp_out_Drawing)
    {  
	
  	WSession ses = (WSession)session;
	
	try 
	    {
		if (model == null || model.GetType() != ModelType.MDL_DRAWING)
		    throw new Exception("Method: otkDrawingCreateSheetAndViews \nMsg: Current model is not a drawing");
		
		Drawing drawing = (Drawing)model;
		
		fp_out_Drawing.write("The drawing has " + drawing.List2DViews().getarraysize() + " views");
		fp_out_Drawing.newLine();
		
		int sheetNo = drawing.AddSheet();
		drawing.SetCurrentSheetNumber (sheetNo);
		
		/*--------------------------------------------------------------------*\
		  Find the solid model, if it is in session
		 \*--------------------------------------------------------------------*/
		ModelDescriptor mdlDescr = null;
		try 
		    {
			mdlDescr = pfcModel.ModelDescriptor_CreateFromFileName (solidName);
		    }
		
		catch (Exception e)
		    {
			System.out.println("Exception: " + e);
		    }
		
		Model solidMdl = ses.GetModelFromDescr (mdlDescr);
		
		if (solidMdl == null)
		    {
		/*--------------------------------------------------------------------*\
		  If it is not found, try to retieve the solid model.
		  (If the model cannot be retrieved, pfcXToolkitError will 
		  be thrown and caught at the end of this function.)
		 \*--------------------------------------------------------------------*/
			solidMdl = ses.RetrieveModel (mdlDescr);
		    }
		/*--------------------------------------------------------------------*\
		  Try to add it to the drawing
		 \*--------------------------------------------------------------------*/
		try
		    {
			drawing.AddModel (solidMdl);
		    } 
		
		catch (Exception e)
		    {
			System.out.println("Exception: " + e);
		    }
		
		
		/*--------------------------------------------------------------------*\
		  Create a general view from the Z axis direction at a predefined location
		 \*--------------------------------------------------------------------*/
		Matrix3D matrix = Matrix3D.create();
		for (int i = 0; i < 4; i++)
		    for (int j = 0; j < 4; j++)
			{
			    if (i == j)
				matrix.set (i, j, 1.0);
			    else
				matrix.set (i, j, 0.0);
			}
		
		Transform3D transf = pfcBase.Transform3D_Create (matrix);
		
		Point3D pos = Point3D.create();
		pos.set (0, 200.0);
		pos.set (1, 600.0);
		pos.set (2, 0.0);
		
		GeneralViewCreateInstructions instrs =
		    pfcView2D.GeneralViewCreateInstructions_Create (solidMdl, sheetNo,
								    pos, transf);
		
		View2D genView = 
		    drawing.CreateView (instrs);
		
		genView.SetName("Front View");		
		
		/*--------------------------------------------------------------------*\
		  Get the position and size of the new view
		 \*--------------------------------------------------------------------*/
		
		double element = 0;
		int count = 1;
		
		fp_out_Drawing.write( "\n\tThe Transformation Matrix:\n");
		fp_out_Drawing.newLine();
		
		for (int i = 0; i < 4; i++)
		    {
			fp_out_Drawing.write("\t");
			for (int j = 0; j < 4; j++)
			    {
				if (i == j)
				    {
					element = matrix.get (i, j);
					fp_out_Drawing.write(element + "\t");
				    }
				else
				    {
					element = matrix.get (i, j);
					fp_out_Drawing.write(element + "\t");
				    }  
			    }
			fp_out_Drawing.newLine();
		    }
		
		Outline3D outline = genView.GetOutline();
		
		String name = genView.GetName();
		
		ViewDisplay display =  genView.GetDisplay();
		
		boolean RemoveQuiltHiddenLines =  display.GetRemoveQuiltHiddenLines();
		
		boolean ShowConceptModel = display.GetShowConceptModel(); 
		
		boolean ShowWeldXSection = display.GetShowWeldXSection();
		
		CableDisplayStyle cableDispStyle = display.GetCableStyle();
		
		DisplayStyle dispStyle = display.GetStyle();
		
		TangentEdgeDisplayStyle tanEdgeDispStyle = display.GetTangentStyle();
		
		boolean IsBackGround = genView.GetIsBackground();
		
		boolean IsScaleUserdefined = genView.GetIsScaleUserdefined();
		
		boolean IsViewdisplayLayerDependent = genView.GetIsViewdisplayLayerDependent();
		
		int sheetno = genView.GetSheetNumber();
		
		double x = pos.get(0);
		double y = pos.get(1);
		double z = pos.get(2);
		
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "\t\t\tView " + (count++) );
		fp_out_Drawing.newLine();
		fp_out_Drawing.newLine();	
		fp_out_Drawing.write( "Type: " + name);
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "Sheet number: " + sheetno);
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "The co-ordinates for Point3D are: (" + x + "," + y + "," + z + ")");
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "View is in Back Ground: " + IsBackGround);
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "Scale is user defined: " + IsScaleUserdefined);
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "View is Display Layer Dependent: " + IsViewdisplayLayerDependent);	
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "Quilt Hidden Lines are removed: " + RemoveQuiltHiddenLines);	
		fp_out_Drawing.newLine();
		
		DispStyle(dispStyle.getValue(), fp_out_Drawing);
		fp_out_Drawing.newLine();
		CableDispStyle(cableDispStyle.getValue(), fp_out_Drawing);	
		fp_out_Drawing.newLine();
		TanEdgeDispStyle(tanEdgeDispStyle.getValue(), fp_out_Drawing);
		fp_out_Drawing.newLine();
		fp_out_Drawing.newLine();
		
		/*--------------------------------------------------------------------*\
		  Create a projected view to the right of the general view
		  \*--------------------------------------------------------------------*/
		pos.set (0,  outline.get(1).get(0) + 
			 (outline.get(1).get(0) - outline.get(0).get(0))  );
		pos.set (1, (outline.get(0).get(1) + outline.get(1).get(1))/2);
		
		ProjectionViewCreateInstructions pInstrs =
		    pfcView2D.ProjectionViewCreateInstructions_Create (genView, pos);
		
		View2D projViewRight = drawing.CreateView ((View2DCreateInstructions)pInstrs);
		
		projViewRight.SetName("Left Hand Side View");
		
		name = projViewRight.GetName();
		
		display =  genView.GetDisplay();
		
		RemoveQuiltHiddenLines =  display.GetRemoveQuiltHiddenLines();
		
		ShowConceptModel = display.GetShowConceptModel(); 
		
		ShowWeldXSection = display.GetShowWeldXSection();
		
		cableDispStyle = display.GetCableStyle();
		
		dispStyle = display.GetStyle();
		
		tanEdgeDispStyle = display.GetTangentStyle();	
		
		IsBackGround = genView.GetIsBackground();
		
		IsScaleUserdefined = genView.GetIsScaleUserdefined();
		
		IsViewdisplayLayerDependent = genView.GetIsViewdisplayLayerDependent();
		
		sheetno = projViewRight.GetSheetNumber();
		
		x = pos.get(0);
		y = pos.get(1);
		z = pos.get(2);	
		
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "\t\t\tView " + (count++) );
		fp_out_Drawing.newLine();	
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "Type: " + name);
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "Sheet number: " + sheetno);
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "The co-ordinates for Point3D are: (" + x + "," + y + "," + z + ")");
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "View is in Back Ground: " + IsBackGround);
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "Scale is user defined: " + IsScaleUserdefined);	
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "View is Display Layer Dependent: " + IsViewdisplayLayerDependent);		
		fp_out_Drawing.newLine();
		fp_out_Drawing.write( "Quilt Hidden Lines are removed: " + RemoveQuiltHiddenLines);
		fp_out_Drawing.newLine();
		
		DispStyle(dispStyle.getValue(), fp_out_Drawing);	
		fp_out_Drawing.newLine();
		CableDispStyle(cableDispStyle.getValue(), fp_out_Drawing);		
		fp_out_Drawing.newLine();
		TanEdgeDispStyle(tanEdgeDispStyle.getValue(), fp_out_Drawing);
		fp_out_Drawing.newLine();
		fp_out_Drawing.newLine();	
		
		
		
		/*--------------------------------------------------------------------*\
		  Create a projected view below the general view
		  \*--------------------------------------------------------------------*/
		pos.set (0, (outline.get(0).get(0) + outline.get(1).get(0))/2);
		pos.set (1,  outline.get(0).get(1) - 
			 (outline.get(1).get(1) - outline.get(0).get(1))  );
		
		pInstrs =
		    pfcView2D.ProjectionViewCreateInstructions_Create (genView, pos);
		
    View2D projViewTop = drawing.CreateView ((View2DCreateInstructions)pInstrs);
    
    projViewTop.SetName("Top View");
    
    name = projViewTop.GetName();
    
    IsBackGround = genView.GetIsBackground();
    
    IsScaleUserdefined = genView.GetIsScaleUserdefined();
    
    IsViewdisplayLayerDependent = genView.GetIsViewdisplayLayerDependent();
    
    sheetno = projViewTop.GetSheetNumber();
    
    x = pos.get(0);
    y = pos.get(1);
    z = pos.get(2);
    
    fp_out_Drawing.newLine();
    fp_out_Drawing.write( "\t\t\tView " + (count++) );
    fp_out_Drawing.newLine();
    fp_out_Drawing.newLine();	
    fp_out_Drawing.write( "Type: " + name);
    fp_out_Drawing.newLine();
    fp_out_Drawing.write( "Sheet number: " + sheetno);
    fp_out_Drawing.newLine();
    fp_out_Drawing.write( "The co-ordinates for Point3D are: (" + x + "," + y + "," + z + ")");
    fp_out_Drawing.newLine();
    fp_out_Drawing.write( "View is in Back Ground: " + IsBackGround);
    fp_out_Drawing.newLine();
    fp_out_Drawing.write( "Scale is user defined: " + IsScaleUserdefined);
    fp_out_Drawing.newLine();
    fp_out_Drawing.write( "View is Display Layer Dependent: " + IsViewdisplayLayerDependent);		
    fp_out_Drawing.newLine();
    fp_out_Drawing.write( "Quilt Hidden Lines are removed: " + RemoveQuiltHiddenLines);
    fp_out_Drawing.newLine();
    
    DispStyle(dispStyle.getValue(), fp_out_Drawing);
    fp_out_Drawing.newLine();
    CableDispStyle(cableDispStyle.getValue(), fp_out_Drawing);	
    fp_out_Drawing.newLine();
    TanEdgeDispStyle(tanEdgeDispStyle.getValue(), fp_out_Drawing);
    fp_out_Drawing.newLine();
    fp_out_Drawing.newLine();
    
    fp_out_Drawing.write("Now the drawing has " + drawing.List2DViews().getarraysize() + " views" );
    
	    }
	
	catch (Exception e)
	    {
		System.out.println("Exception: " + e);
	    }
    }
    
    void DispStyle(int x, BufferedWriter fp_out_Drawing)
    {
	try
	    {
		switch(x)
		    {
		    case DisplayStyle._DISPSTYLE_DEFAULT : fp_out_Drawing.write("Display Style: DISPSTYLE_DEFAULT");
			break;
			
		    case DisplayStyle._DISPSTYLE_WIREFRAME : fp_out_Drawing.write("Display Style: DISPSTYLE_WIREFRAME");
			break;
			
		    case DisplayStyle._DISPSTYLE_HIDDEN_LINE : fp_out_Drawing.write("Display Style: DISPSTYLE_HIDDEN_LINE");
			break;
			
		    case DisplayStyle._DISPSTYLE_NO_HIDDEN : fp_out_Drawing.write("Display Style: DISPSTYLE_NO_HIDDEN ");
			break;
			
		    case DisplayStyle._DISPSTYLE_FOLLOW_ENVIRONMENT : fp_out_Drawing.write("Display Style: DISPSTYLE_FOLLOW_ENVIRONMENT");		
			break;
			
		    case DisplayStyle._DISPSTYLE_SHADED : fp_out_Drawing.write("Display Style: DISPSTYLE_SHADED");
			break;
		    }	
	    }
	
	catch (Exception e)
	    {
		System.out.println("Exception: " + e);
	    }		
	
    }
    
    void CableDispStyle(int y, BufferedWriter fp_out_Drawing)
    {
	try
	    {
		switch(y)
		    {
		    case CableDisplayStyle._CABLEDISP_DEFAULT : fp_out_Drawing.write("Cable Display Style: CABLEDISP_DEFAULT");
			break;
			
		    case CableDisplayStyle._CABLEDISP_CENTERLINE : fp_out_Drawing.write("Cable Display Style: CABLEDISP_CENTERLINE");
			break;
			
		    case CableDisplayStyle._CABLEDISP_THICK : fp_out_Drawing.write("Cable Display Style: CABLEDISP_THICK");
			break;
		    }	
	    }
	
	catch (Exception e)
	    {
		System.out.println("Exception: " + e);
	    }		
	
    }
    
    void TanEdgeDispStyle(int z, BufferedWriter fp_out_Drawing)
    {
	try
	    {
		switch(z)
		    {
		    case TangentEdgeDisplayStyle._TANEDGE_DEFAULT : fp_out_Drawing.write("Tangent Edge Display: TANEDGE_DEFAULT");
			break;
			
		    case TangentEdgeDisplayStyle._TANEDGE_NONE : fp_out_Drawing.write("Tangent Edge Display: TANEDGE_NONE");
			break;
			
		    case TangentEdgeDisplayStyle._TANEDGE_CENTERLINE : fp_out_Drawing.write("Tangent Edge Display: TANEDGE_CENTERLINE");
			break;
			
		    case TangentEdgeDisplayStyle._TANEDGE_PHANTOM : fp_out_Drawing.write("Tangent Edge Display: TANEDGE_PHANTOM");				  
			break;
			
		    case TangentEdgeDisplayStyle._TANEDGE_DIMMED : fp_out_Drawing.write("Tangent Edge Display: TANEDGE_DIMMED");
			break;
			
		    case TangentEdgeDisplayStyle._TANEDGE_SOLID : fp_out_Drawing.write("Tangent Edge Display: TANEDGE_SOLID");
			break;
			
		    }				  
	    }
	
	catch (Exception e)
	    {
		System.out.println("Exception: " + e);
	    }		
	
    }
}
